﻿google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);

var _areas = [
{n:'Ostrava', v:['Ostrava-Fifejdy', 'Ostrava-Přívoz', 'Ostrava-Zábřeh']},
{n:'Sever', v:['Bohumín', 'Karviná', 'Orlová', 'Věřňovice']},
{n:'Východ', v:['Český Těšín', 'Havířov', 'Třinec-Kanada', 'Třinec-Kosmos']},
{n:'Jih', v:['Frýdek-Místek', 'Studénka']},
{n:'Západ', v:['Opava-Kateřinky']}
];

var _data = _data_week;
var _start = _start_date;
var _stepHours = 1;
var _currentPoint = 'Celý kraj';

$(document).ready(function(){
	$(function() {
		$("#timePeriod ul").selectable();
	});

	$('#switchWeek').addClass('ui-selected');
	$('#areas ul:first li').addClass('currentPoint');

	$('#areas li').click(function(){
		var point = $(this).text();
		if (_currentPoint == point) {
			return false;
		}
		$('li').removeClass('currentPoint');
		$(this).addClass('currentPoint');
		if ($(this).hasClass('area')) {
			$(this).siblings().addClass('currentPoint');
		}
		_currentPoint = point;
		drawChart();
	});

	$('#timePeriod li').click(function(){
		if ($(this).attr('id') == 'switchWeek') {
			_data = _data_week;
			_start = _start_date;
			_stepHours = 1;
		}
		else {
			_data = _data_month;
			_start = _start_date_month;
			_stepHours = 4;
		}
		drawChart();
	});
});

function generateData() {
	var result = [];

	var data = findData(_currentPoint, _data);
	if (data == null) {
		var area = findData(_currentPoint, _areas);
		if (area == null) {
			data = computeAverage(_data);
		}
		else {
			data = computeAverageArea(area);
		}
	}

	var movingAverage = [];
	for (var i = 0; i < data.v.length; i++) {
		var date = new Date(_start.getFullYear(), _start.getMonth(), _start.getDate(), _start.getHours() + (i * _stepHours), 0, 0, 0);
		var v = data.v[i];
		var value = v <= 0 ? null : v;
		var moving24 = null;
		if (i >= 24 / _stepHours) {
			moving24 = computeMovingAverage(movingAverage);
		}
		movingAverage[i % (24 / _stepHours)] = value;
		result[i] = [date, 90, 50, value, moving24];
	}
	
	return result;
}

function findData(pointName, array) {
	for (var i = 0; i < array.length; i++) {
		var data = array[i];
		if (data.n == pointName) return data;
	}
	return null;
}

function computeAverageArea(area) {
	var dataArr = [];
	for (var j = 0; j < area.v.length; j++) {
		var pointName = area.v[j];
		var data = findData(pointName, _data);
		dataArr[j] = data;
	}

	var result = computeAverage(dataArr);
	return result;
}

function computeAverage(dataArray) {
	var result = {v:[]};
	var len = _data[0].v.length;
	for (var i = 0; i < len; i++) {
		var sum = 0;
		var count = 0;
		for (var j = 0; j < dataArray.length; j++) {
			var data = dataArray[j];
			if (data != null) {
				sum += data.v[i];
				count++;
			}
		}
		if (count == 0)
			result.v[i] = 0
		else
			result.v[i] = Math.round(sum / count);
	}

	return result;
}

function computeMovingAverage(movingAverage) {
	var sum = 0;
	var count = 0;
	for (var i = 0; i < movingAverage.length; i++) {
		var v = movingAverage[i];
		if (v != null) {
			sum += v;
			count++;
		}
	}
	
	return Math.round(sum / count);
}

function drawChart() {
	var dt = new google.visualization.DataTable();

	dt.addColumn('datetime', 'time');
	dt.addColumn('number', 'Limit pro okamžité hodnoty (90)');
	dt.addColumn('number', 'Limit pro 24hodinový průměr (50)');
	dt.addColumn('number', _currentPoint + ' - okamžité hodnoty');
	dt.addColumn('number', _currentPoint + ' - 24hodinový průměr');

	var rows = generateData();
	dt.addRows(rows);

	var options = {'width':1600, 'height':650, interpolateNulls:true,
		chartArea:{left:60, top:20},
		hAxis:{format:'d.M. HH:mm', slantedText:true, slantedTextAngle:90, showTextEvery:_stepHours}, // showTextEvery:24/_stepHours
		vAxis:{maxValue:100, minValue:0, title:'PM10'},
		series:[
			{color:'000088', lineWidth:1}, {color:'bb0000', lineWidth:1},
			{color:'blue', lineWidth:2}, {color:'red', lineWidth:2}
		]
	};

	var chart = new google.visualization.LineChart(document.getElementById('chart'));
	chart.draw(dt, options);
}
